/*
 * Copyright (c) 2023 IUV Inc. All rights reserved.
 * 项目名称：IUV TXY
 * 文件名：RegistController.java
 * Date：23-10-10 下午2:09
 * Author：Will
 */

package com.iuv.baike.controller;

import com.iuv.baike.common.CommonOperateCode;
import com.iuv.baike.dto.StatusInfo;
import com.iuv.baike.service.DemoBaikeStatusService;
import com.iuv.baike.util.RespondUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

import static com.iuv.baike.util.RespondUtil.*;

/**
 * RegistController
 * 号码状态控制器
 *
 * @author Will
 * @date 16:33
 */
/**
 * TODO 问题二 申明自己为处理url为 "/status" 的Controller
 */
@Controller
@RequestMapping("status ")/*
/*
 * END
 */
public class BaiKeStatusController {
    private static Logger logger = LoggerFactory.getLogger(BaiKeStatusController.class);
    /**
     * TODO 问题三 引入业务接口 DemoBaikeStatusService
     */
    @Autowired
    private DemoBaikeStatusService demoBaikeStatusService;



    /*
     * END
     */

    /**
     * 根据手机号码查询号码状态
     *
     * @param mobile
     * @return
     */
    @RequestMapping("queryNumberStatus")
    @ResponseBody
    public Map<String, Object> queryNumberStatus(@RequestParam String mobile) {

        logger.info("查询号码状态，手机号码为：" + mobile);

        mobile = StringUtils.trimToNull(mobile);
        StatusInfo statusInfo = null;
        Map<String, Object> repond = null;
        /**
         * TODO 问题四 调用业务接口 DemoBaikeStatusService 实现如下功能：
         * 1. 如果手机号为空，返回MOBILE_NOT_NULL业务错误信息
         * 2. 先查询本地数据库
         * 3. 未查询到状态信息，再通过云端进行查询
         * 4. 都查询不到，则返回QUERY_ERROR业务错误信息
         * 5. 查询到，则返回操作成功信息，并携带手机状态信息
         */
        if (mobile == null) {
            return RespondUtil.respondFail(CommonOperateCode.SysCode.NOBILE_NOT_NOLL, CommonOperateCode.SysMsg.NOBILE_NOT_NOLL);
        }
        statusInfo = demoBaikeStatusService.queryByMobile(mobile);
        if (statusInfo == null) {
            statusInfo = demoBaikeStatusService.queryOverYun(mobile);
        }
        if (statusInfo == null) {
            return RespondUtil.respondFail(CommonOperateCode.SysCode.QUERY_ERROR, CommonOperateCode.SysMsg.QUERY_ERROR);
        }
        repond = respondSuccess(CommonOperateCode.SysCode.QUERY_ERROR, CommonOperateCode.SysMsg.QUERY_ERROR);
        repond.put("statusInfo", statusInfo);
        return repond;
    }
}
    /*
     * END
     */
